<?php
namespace app\admin\model\template;

use think\facade\Log;
use think\facade\Request;

class Ticket
{



    /**
     * 架构函数
     *
     * @access public
     * @param array $data
     *            数据
     */
    public function __construct()
    {


       // $this->template_id = sysconfig("template", "withdraw_result_template");
       // $this->template_remark = sysconfig("template", "withdraw_result_remark");
    }

    /**
     * 获取模板发送数据
     */
    private function getData($data)
    {
        $params['touser'] =$data['openid'];
        $params['template_id'] = $data['template_id'];
        if ( isset($data['url']) &&  ! empty($data['url'])) {
            $params['page'] = $data['url'];
        } else {
            $params['page'] = "/pages/index/index";
        }
        $params['data'] = array(
            'thing1' => array(
                'value' => $data['name'],
                'color' => '#ff510'
            ),
            'amount14' => array(
                'value' => $data['price'],
                'color' => '#ff510'
            ),
            'time5' => array(
                'value' => date('Y-m-d H:i:s'),
                'color' => '#ff510'
            ),
            'thing6' => array(
                'value' => $data['name'],
                'color' => '#ff510'
            ),
        );
        $params = json_encode($params, true);
        return $params;
    }

    public function send($data)
    {
        $token = getAccessToken($data['appid'],$data['appsecret']);
        if (empty($token)) {
            Log::error("WithdrawResult template 获取用户token失败", $this->data);
            return;
        }
        $sendParams = $this->getData($data);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $token;
        $re = wtw_request($url, $sendParams);
        
        Log::write("WithdrawResult template send" . json_encode(array(
            'url' => $url,
            'sendParams' => $sendParams,
            're' => $re
        )), 'info');
        
        return $re;
    }
}